#include "loginwidget.h"

LoginWidget::LoginWidget(QWidget *parent)
    :QWidget (parent)
{
    this->init_window();
    this->init_control();
    this->init_connect();
    this->init_qss();
    UserModel::getInstance()->updateUserAll();
}

LoginWidget::~LoginWidget()
{

}

void LoginWidget::init_window()
{
    this->setWindowTitle("登录界面");
    this->resize(700,700);
}

void LoginWidget::init_control()
{
    //标签
    this->title = new QLabel("登陆界面",this);
    this->title->move(280,70);
    this->title->setStyleSheet("font-size:40px;font-weight:bold");
    this->equipment = new QLabel("设备编号", this);
    this->equipment->move(100,180);
    this->name = new QLabel("警员编号", this);
    this->name->move(100,260);
    this->password = new QLabel("密  码", this);
    this->password->move(100,340);
    this->check = new QLabel("验证码", this);
    this->check->move(100,420);

    //编辑框
    this->equipEdit = new QLineEdit("", this);
    //从表中获取设备编号

    this->equipEdit->move(250,170);
    this->equipEdit->setReadOnly(true);
    this->nameEdit = new QLineEdit("", this);
    this->nameEdit->move(250,250);
    this->nameEdit->setEchoMode(QLineEdit::Normal);
    this->passwordEdit = new QLineEdit("", this);
    this->passwordEdit->move(250,330);
    this->passwordEdit->setEchoMode(QLineEdit::Password);
    this->passwordEdit->setStyleSheet("width:260px");
    //默认是密文
    this->isPassword = true;

//    this->checkEdit = new QLineEdit("", this);
//    this->checkEdit->move(250,410);
//    this->checkEdit->setStyleSheet("width:250px");

    //按钮
    this->checkBtn = new QPushButton("", this);
    this->checkBtn->move(570,400);
    this->checkBtn->setStyleSheet("width:50px;height:50px");
    this->checkBtn->setIcon(QIcon(":/img/check.png"));

    this->changeBtn = new QPushButton("", this);
    this->changeBtn->move(520,330);
    this->changeBtn->setStyleSheet("width:30px;height:30px");
    this->changeBtn->setIcon(QIcon(":/img/ma.png"));

    this->checkEdit = new QLineEdit("", this);
    this->checkEdit->move(250,400);

//    this->checkBtn->
    this->enterBtn = new QPushButton("确定", this);
    this->enterBtn->move(150, 540);
    this->backBtn = new QPushButton("返回", this);
    this->backBtn->move(450,540);
    this->input = new QLabel(this);
    this->input->move(275,450);
    this->input->setFixedWidth(200);


    //获取表中最后一行的设备信息
    char **qres=nullptr;
    int col=0,row=0;
    int res = ConfigureModel::getInstance()->selectLastConfigure(qres, row, col);
    if(res==0)
    {
        for(int i=0;i<=row;i++)
        {
            qDebug() << i;
        }
        this->equipEdit->setText(qres[1*col]);
    }
    else {
        QMessageBox::warning(this, "登陆界面", "没有完成设置");
        return ;
    }

}

void LoginWidget::init_connect()
{
    connect(this->checkBtn, SIGNAL(clicked()), this, SLOT(CheckLogin()));
    connect(this->changeBtn, SIGNAL(clicked()), this, SLOT(setMode()));
    connect(this->enterBtn, SIGNAL(clicked()), this, SLOT(doLogin()));
    connect(this->backBtn, SIGNAL(clicked()), this, SIGNAL(backToMain1()));

}

void LoginWidget::init_qss()
{
    QFile file(":/img/login.qss");
    if(file.open(QFile::ReadOnly))
    {
        this->setStyleSheet(file.readAll());
        file.close();
    }
}

void LoginWidget::CheckLogin()
{
    //用一个字符串接收随机生成的验证码
    QString code;
    int num;
    code.clear();
    for(int i=0;i<4;++i){
        num = qrand()%3;//通过随机数决定生成数组、大写还是小写字母
        if(num==0)
        {
            //数字
            code += QString::number(qrand()%10);
        }
        else if(num==1)
        {
            //大写字母
            int temp = 'A';
            code += static_cast<QChar>(temp + qrand()%26);
        }
        else if(num==2)
        {
            //小写字母
            int temp = 'a';
            code += static_cast<QChar>(temp + qrand()%26);
        }
    }
    qDebug() << code;
    //将按钮中的内容换成生成的验证码

    this->input->setText(code);
//    disconnect();
//    this->sl = new sliderscreen;
//    this->sl->show();
}

void LoginWidget::setMode()
{
    if(this->isPassword){
           this->changeBtn->setIcon(QIcon(":/img/mi.png"));
           this->passwordEdit->setEchoMode(QLineEdit::Normal);
       }
       else{
           this->changeBtn->setIcon(QIcon(":/img/ma.png"));
           this->passwordEdit->setEchoMode(QLineEdit::Password);
       }
    this->isPassword = !this->isPassword;
}

//点击按钮登录
void LoginWidget::doLogin()
{
//    //先验证是否有编辑框为空
    if(this->nameEdit->text().isEmpty()||this->checkEdit->text().isEmpty()||this->passwordEdit->text().isEmpty())
    {
        QMessageBox::warning(this, "登录界面", "编辑框为空");
        return ;
    }
        //再判断验证码是否正确
    if(this->checkEdit->text().toLower()!=this->input->text().toLower())
    {
        QMessageBox::warning(this, "登陆界面", "验证码错误");
        return ;
    }



    int res = UserModel::getInstance()->selectUserByNameAndPwd(this->nameEdit->text(), MD5Encryption(this->passwordEdit->text()));
    if(res==0)
    {
        QMessageBox::information(this,"登录界面", "登陆成功");
        UserModel::getInstance()->updateUserByName(this->nameEdit->text());

        //获取当前日期和时间
            QDateTime currentDateTime = QDateTime::currentDateTime();
            //获取当前时间
            QTime currentTime = currentDateTime.time();
            //获取当前日期
            QDate currentDate = currentDateTime.date();
            QString date = currentDateTime.toString("yyyy_MM_dd_hh_mm_ss");
            QString datetime = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
        //    QString filename = "../photo/" + date + ".jpg";
            QString filename = date + ".jpg";
        LogModel::getInstance()->InsertLog(datetime, this->equipEdit->text(), this->nameEdit->text(), "登录");
        this->clearForm();

        emit backToMain2();
        qDebug() << "bbbbbbbbbbbbb";
    }
    else {
        QMessageBox::warning(this, "登陆界面", "登录失败");
        this->clearForm();
    }
}

//void LoginWidget::closeEvent(QCloseEvent *event)
//{
//    QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Confirm Exit",
//                tr("Are you sure you want to exit?\n"),
//                QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
//                QMessageBox::Yes);

//    if (resBtn != QMessageBox::Yes) {

//        event->ignore();
//    } else {
//        UserModel::getInstance()->updateUserAll();
//        event->accept();
//    }

//}

void LoginWidget::clearForm()
{
    this->nameEdit->clear();
    this->passwordEdit->clear();
    this->checkEdit->clear();
}

QString LoginWidget::MD5Encryption(const QString &str)
{
    QCryptographicHash md5(QCryptographicHash::Md5);
    md5.addData(str.toLocal8Bit());
    qDebug() << md5.result().toHex();
    return md5.result().toHex();
}
